home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / doPaintEffectsToPoly.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  35.0 KB  |  928 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //  Alias|Wavefront Script File
  18. //  MODIFY THIS AT YOUR OWN RISK
  19. //
  20. //  Description:
  21. //      Functions called from "PaintEffectsToPoly".
  22. //
  23.  
  24. // Description:
  25. // return the full path to the passed in filename
  26. proc string getPaintEffectsFilename( string $fname )
  27. {
  28.     string $outPath = $fname;
  29.     if( filetest( "-r", $outPath ) ){
  30.         return $outPath;
  31.     }  
  32.     string $visorBase = `getenv "MAYA_LOCATION"`;
  33.     if ($visorBase != "") {
  34.         // Set the brushImage directory path appropriately.
  35.         $outPath = ($visorBase + "/brushImages/" + $fname);
  36.         if( filetest( "-r", $outPath ) ){
  37.             return $outPath;
  38.         }
  39.     }
  40.  
  41.     return $fname; // can't find file in brushImages dir, so simply return the original name
  42. }
  43.  
  44. proc string getShaderMaterialInfo( string $shadingGroup ){
  45.     string $con[] = `listConnections $shadingGroup`;
  46.     int $i;
  47.     for( $i=0; $i < size( $con ); $i++ ){
  48.         if( "materialInfo" == nodeType( $con[$i] ) ){
  49.             return( $con[$i] );
  50.         }
  51.     }
  52.     return "";
  53. }
  54.  
  55.  
  56. proc copyRamp(string $source, string $sourceAttr,  string $target, string $targetAttr, int $isColor, float $scale){
  57.     // for now we assume that source has more entries than target does
  58.     string $sourceBase = ($source + "." + $sourceAttr);
  59.     string $targetBase = ($target + "." + $targetAttr);
  60.     int $numEntries = `getAttr -s $sourceBase`;
  61.     int $i, $ival;
  62.     float $fval;
  63.     for( $i = 0; $i < $numEntries; $i++ ){
  64.         string $sB = ($sourceBase + "["+$i+"]."+$sourceAttr);
  65.         string $tB = ($targetBase + "["+$i+"]."+$targetAttr);
  66.         $fval = getAttr($sB + "_Position");
  67.         $ival = getAttr($sB + "_Interp");
  68.         setAttr ($tB + "_Position") $fval;
  69.         setAttr ($tB + "_Interp") $ival;
  70.         if( $isColor){
  71.             $fval = getAttr($sB + "_ColorR");
  72.             setAttr ($tB + "_ColorR") ($fval * $scale);
  73.             $fval = getAttr($sB + "_ColorG");
  74.             setAttr ($tB + "_ColorG") ($fval * $scale);
  75.             $fval = getAttr($sB + "_ColorB");
  76.             setAttr ($tB + "_ColorB") ($fval * $scale);
  77.         } else {
  78.             $fval = getAttr($sB + "_FloatValue");
  79.             setAttr ($tB + "_FloatValue") ($fval * $scale);
  80.         }
  81.     }
  82. }
  83.  
  84. proc createAndAssignShader(string $shaderName, float $color1[], float $color2[],
  85.             float $incand[], float $specularColor[], float $specular, float $specularPower, 
  86.             float $glowIntensity,
  87.             float $translucence, float $transparency[], float $softness,
  88.             int $texType, int $colorTex, int $opTex, string $texName, 
  89.             float $texColor1[], float $texColor2[], float $smear, float $smearU, float $smearV,
  90.             float $fractalRatio, float $fractalAmplitude, float $fractalThreshold,
  91.             float $repeatU, float $repeatV, float $offsetU, float $offsetV,
  92.             float $reflectivity, int $textureSpecular, int $isFlat, string $brush,
  93.             string $mesh )
  94.             
  95. {
  96.     // create shader to match rendering by brush
  97.     float $spec[3];
  98.     $spec[0] = $specularColor[0] * $specular;
  99.     $spec[1] = $specularColor[1] * $specular;
  100.     $spec[2] = $specularColor[2] * $specular;
  101.     int $hasSpecular = ( $spec[0] > 0.05 || $spec[1] > 0.05 || $spec[2] > 0.05 );
  102.     int $doSoftness = ($softness < -0.05 || $softness > 0.1 );
  103.     int $doSmear = ($smearU > 0.01 || $smearV > 0.01 );
  104.     int $hasIncand = ($incand[0] > 0.01 || $incand[1] > 0.01 || $incand[2] > 0.01 );
  105.     int $doReflection = ($reflectivity > 0.05 );
  106.     float $colorDif = abs($color1[0] - $color2[0]) + abs($color1[1] - $color2[1]) + abs($color1[2] - $color2[2]);
  107.     int $colorRamp = ($colorDif > 0.05);
  108.     int $softnessTexture = $doSoftness && $isFlat;
  109.     int $opacityTex = $opTex || $softnessTexture; 
  110.     int $hasTransparency = $doSoftness || $opacityTex 
  111.             || $transparency[0] > 0.1 || $transparency[1] > 0.1 || $transparency[2] > 0.1;
  112.  
  113.     int $useRampShader = ($doSoftness && ($softnessTexture == 0)) || $doReflection;
  114.     string $material;
  115.     if( $useRampShader ){
  116.         $material = `shadingNode -asShader -name $shaderName "rampShader"`;
  117.         setAttr ($material + ".incandescence[0].incandescence_Color") -type double3 $incand[0] $incand[1] $incand[2];
  118.         if( $hasTransparency ){
  119.             setAttr ($material + ".transparency[0].transparency_Color") -type double3 
  120.                         $transparency[0] $transparency[1] $transparency[2];
  121.     
  122.             if( $hasSpecular ){
  123.                 // paint effects modulates specular based on transparency
  124.                 setAttr ($material + ".specularColor[0].specularColor_Color") -type double3 
  125.                         ((1.0-$transparency[0])*$spec[0]) 
  126.                         ((1.0-$transparency[1])*$spec[1]) 
  127.                         ((1.0-$transparency[2])*$spec[2]);
  128.             } else {
  129.                 setAttr ($material + ".specularColor[0].specularColor_Color") -type double3 0 0 0;
  130.             }
  131.             if( $doSoftness && ($isFlat == 0)){
  132.                 if( $softness > 0 ){
  133.                     setAttr ($material + ".transparency[1].transparency_Color") -type double3 1.0 1.0 1.0;
  134.                     setAttr ($material + ".transparency[1].transparency_Position") 0.0;
  135.                     $softness = sqrt( $softness );
  136.                     setAttr ($material + ".transparency[0].transparency_Position") $softness;
  137.                 } else {
  138.                     setAttr ($material + ".transparency[1].transparency_Color") -type double3 1.0 1.0 1.0;
  139.                     $softness = (1+$softness);
  140.                     setAttr ($material + ".transparency[1].transparency_Position") 1.0;
  141.                     setAttr ($material + ".transparency[0].transparency_Position") ($softness * 0.95);
  142.                 }
  143.                 setAttr ($material + ".transparency[1].transparency_Interp") 1.0;
  144.             }
  145.         } else {
  146.             setAttr ($material + ".specularColor[0].specularColor_Color") -type double3 $spec[0] $spec[1] $spec[2];
  147.         }
  148.         setAttr ($material + ".eccentricity") (1.0/sqrt($specularPower));
  149.         setAttr ($material + ".specularity") 1.0;
  150.         removeMultiInstance -break true ($material +".specularRollOff[1]");
  151.         if( $doReflection ){
  152.             copyRamp( $brush, "environment", $material, "environment", true, 1.0 );
  153.             copyRamp( $brush, "reflectionRolloff", $material, "reflectivity", false, $reflectivity);
  154.         }
  155.     } else {
  156.         $material = `shadingNode -asShader -name $shaderName "phong"`;
  157.         setAttr ($material + ".incandescence") -type double3 $incand[0] $incand[1] $incand[2];
  158.         if( $hasTransparency ){
  159.             setAttr ($material + ".transparency") -type double3 
  160.                         $transparency[0] $transparency[1] $transparency[2];
  161.     
  162.             if( $hasSpecular ){
  163.                 // paint effects modulates specular based on transparency
  164.                 setAttr ($material + ".specularColor") -type double3 
  165.                         ((1.0-$transparency[0])*$spec[0]) 
  166.                         ((1.0-$transparency[1])*$spec[1]) 
  167.                         ((1.0-$transparency[2])*$spec[2]);
  168.             } else {
  169.                 setAttr ($material + ".specularColor") -type double3 0 0 0;
  170.             }
  171.         } else {
  172.             setAttr ($material + ".specularColor") -type double3 $spec[0] $spec[1] $spec[2];
  173.         }
  174.         setAttr ($material + ".cosinePower") $specularPower;
  175.     }
  176.  
  177.     
  178.     setAttr ($material + ".diffuse") (1.0 - $translucence);
  179.     setAttr ($material + ".translucence") $translucence;
  180.     setAttr ($material + ".translucenceDepth") 1.0;
  181.     setAttr ($material + ".translucenceFocus") 0.0;
  182.     setAttr ($material + ".glowIntensity") $glowIntensity;
  183.     string $ramp;
  184.     // if the start end colors are not the same then create a ramp to simulate this
  185.     if( $colorRamp ){
  186.         $ramp = `shadingNode -asTexture ramp`;
  187.         removeMultiInstance -break true ($ramp + ".colorEntryList[2]");
  188.         // set ramp colors to match color 1 and 2 of brush
  189.         setAttr ($ramp+".colorEntryList[0].color") -type double3 $color1[0] $color1[1] $color1[2] ;    
  190.         setAttr ($ramp+".colorEntryList[1].color") -type double3 $color2[0] $color2[1] $color2[2] ;    
  191.         setAttr ($ramp+".colorEntryList[1].position") 1.0 ;    
  192.         if( $useRampShader ){
  193.             connectAttr -f ($ramp+".outColor") ($material+".color[0].color_Color");
  194.         } else {
  195.             connectAttr -f ($ramp+".outColor") ($material+".color");
  196.         }
  197.     }else {
  198.         if( $useRampShader ){
  199.             setAttr ($material + ".color[0].color_Color")  -type double3 $color1[0] $color1[1] $color1[2];
  200.         } else {
  201.             setAttr ($material + ".color")  -type double3 $color1[0] $color1[1] $color1[2];
  202.         }
  203.     }
  204.     string $tex, $texOp;
  205.     if( $colorTex || $opacityTex ){
  206.             int $needsTransformNode = $repeatU != 1.0 || $repeatV != 1.0 || $offsetU != 0.0 || $offsetV != 0.0;
  207.             float $colorGain[3];     
  208.             if( $colorTex && !$colorRamp){
  209.                 $colorGain = $color1;
  210.             } else {
  211.                 $colorGain[0] = $colorGain[1] = $colorGain[2] = 1.0;
  212.             }
  213.             if( $colorTex || $opTex ){
  214.  
  215.                 if( $texType != 1 && $texType != 2 ){  // not a ramp
  216.                     if( $smear < 0.8 ){
  217.                         $doSmear = false; // transform node smear is only high frequency
  218.                     }
  219.                     if( $doSmear ){
  220.                         $needsTransformNode = true;
  221.                     }
  222.                 }
  223.                 if( $texType == 0 ){  // checker
  224.                     $tex = `shadingNode -asTexture checker`;
  225.                     if( $colorTex ){
  226.                         setAttr ($tex+".color1") -type double3 $texColor1[0] $texColor1[1] $texColor1[2];
  227.                         setAttr ($tex+".color2") -type double3 $texColor2[0] $texColor2[1] $texColor2[2];
  228.                     }
  229.                 } else if( $texType == 1 || $texType == 2){ // U or VRamp
  230.                     $tex = `shadingNode -asTexture ramp`;
  231.                     removeMultiInstance -break true ($tex + ".colorEntryList[2]");
  232.                     if( $colorTex ){
  233.                         setAttr ($tex+".colorEntryList[0].color") -type double3 $texColor2[0] $texColor2[1] $texColor2[2] ;    
  234.                         setAttr ($tex+".colorEntryList[1].color") -type double3 $texColor1[0] $texColor1[1] $texColor1[2] ;    
  235.                     } else {
  236.                         setAttr ($tex+".colorEntryList[0].color") -type double3 0.0 0.0 0.0 ;    
  237.                         setAttr ($tex+".colorEntryList[1].color") -type double3 1.0 1.0 1.0 ;    
  238.                     }
  239.                     setAttr ($tex+".colorEntryList[1].position") 1.0 ;    
  240.                     if( $texType == 2 ){ // V Ramp
  241.                         setAttr ($tex + ".type") 1;
  242.                     }
  243.                     if( $doSmear ){
  244.                         setAttr ($tex + ".noiseFreq") $smear;
  245.                         setAttr ($tex + ".noise") (($smearU + $smearV) * 0.5);
  246.                     }
  247.                 } else if( $texType == 3 ){ // Fractal
  248.                     $tex = `shadingNode -asTexture fractal`;
  249.                     setAttr ($tex + ".ratio") $fractalRatio;
  250.                     setAttr ($tex + ".amplitude") $fractalAmplitude;
  251.                     setAttr ($tex + ".threshold") $fractalThreshold;
  252.                 } else if( $texType == 4 ){ // File
  253.                     $texName = getPaintEffectsFilename( $texName );
  254.                     if( size( $texName ) > 0 ){
  255.                         $tex = `shadingNode -asTexture file`;
  256.                         setAttr ($tex + ".fileTextureName") -type "string" $texName;
  257.                         if( $opTex ){
  258.                             // see if the image has an alpha channel 
  259.                             int $hasAlpha = `getAttr ($tex + ".fileHasAlpha")`;
  260.         
  261.                             if( !$hasAlpha ){
  262.                                 // if there is not an alpha channel don't connect alpha out        
  263.                                 $opTex = false;
  264.                                 if($softnessTexture == false){
  265.                                     $opacityTex = false;
  266.                                 }
  267.                                 if( !$colorTex ){
  268.                                     //  we can delete the texure if there is no use for color        
  269.                                     $needsTransformNode = false;
  270.                                     delete $tex;
  271.                                 }
  272.                             }
  273.                         }
  274.                     } else {
  275.                         $colorTex = false;
  276.                         $opTex = false;
  277.                         if($softnessTexture == false){
  278.                             $opacityTex = false;
  279.                         }
  280.                         $needsTransformNode = false;
  281.                     }
  282.                 } 
  283.                 if( $needsTransformNode ){
  284.                     string $tform = `shadingNode -asUtility place2dTexture`;
  285.                     setAttr ($tform + ".repeatU" ) $repeatV;
  286.                     setAttr ($tform + ".repeatV" ) $repeatU;
  287.                     setAttr ($tform + ".offsetU" ) $offsetV;
  288.                     setAttr ($tform + ".offsetV" ) $offsetU;
  289.                     if( $doSmear && $texType != 1 && $texType != 2 ){
  290.                         setAttr ($tform + ".noiseU" ) ($smearV * 0.2);
  291.                         setAttr ($tform + ".noiseV" ) ($smearU * 0.2);
  292.                     }
  293.                     connectAttr ($tform + ".outUV") ($tex + ".uv");
  294.                     connectAttr ($tform + ".outUvFilterSize") ( $tex + ".uvFilterSize" );
  295.                     if( $texType == 4 ){ // file textures need these extra connections
  296.                         connectAttr -f ($tform + ".coverage") ($tex + ".coverage");
  297.                         connectAttr -f ($tform + ".translateFrame") ($tex + ".translateFrame");
  298.                         connectAttr -f ($tform + ".rotateFrame") ($tex + ".rotateFrame");
  299.                         connectAttr -f ($tform + ".mirrorU") ($tex + ".mirrorU");
  300.                         connectAttr -f ($tform + ".mirrorV") ($tex + ".mirrorV");
  301.                         connectAttr -f ($tform + ".stagger") ($tex + ".stagger");
  302.                         connectAttr -f ($tform + ".wrapU") ($tex + ".wrapU");
  303.                         connectAttr -f ($tform + ".wrapV") ($tex + ".wrapV");
  304.                         connectAttr -f ($tform + ".repeatUV") ($tex + ".repeatUV");
  305.                         connectAttr -f ($tform + ".offset") ($tex + ".offset");
  306.                         connectAttr -f ($tform + ".rotateUV") ($tex + ".rotateUV");
  307.                         connectAttr -f ($tform + ".noiseUV") ($tex + ".noiseUV");
  308.                         connectAttr -f ($tform + ".vertexUvOne") ($tex + ".vertexUvOne");
  309.                         connectAttr -f ($tform + ".vertexUvTwo") ($tex + ".vertexUvTwo");
  310.                         connectAttr -f ($tform + ".vertexUvThree") ($tex + ".vertexUvThree");
  311.                         connectAttr -f ($tform + ".vertexCameraOne") ($tex + ".vertexCameraOne");
  312.                     }
  313.                 }
  314.     
  315.                 if( $colorTex ){
  316.                     if( $texType > 2 ){
  317.                         $texColor1[0] *= $colorGain[0];
  318.                         $texColor1[1] *= $colorGain[1];
  319.                         $texColor1[2] *= $colorGain[2];
  320.                         $texColor2[0] *= $colorGain[0];
  321.                         $texColor2[1] *= $colorGain[1];
  322.                         $texColor2[2] *= $colorGain[2];
  323.                         setAttr ($tex + ".colorOffset") -type double3 $texColor2[0] $texColor2[1] $texColor2[2];
  324.                         $colorGain[0] = $texColor1[0] -$texColor2[0];    
  325.                         $colorGain[1] = $texColor1[1] -$texColor2[1];    
  326.                         $colorGain[2] = $texColor1[2] -$texColor2[2];    
  327.                     } 
  328.                     if( $colorRamp ){
  329.                         connectAttr ($tex + ".outColor") ($ramp + ".colorGain");
  330.                     } else {
  331.                         if( $useRampShader ){
  332.                             connectAttr ($tex + ".outColor") ($material + ".color[0].color_Color");
  333.                         }else{
  334.                             connectAttr ($tex + ".outColor") ($material + ".color");
  335.                         }
  336.                     }
  337.                     setAttr ($tex + ".colorGain") -type double3 $colorGain[0] $colorGain[1] $colorGain[2];
  338.                 }
  339.             }
  340.             $texOp = $tex;
  341.             if( $softnessTexture ){
  342.                 $softTex = `shadingNode -asTexture ramp`;
  343.                 removeMultiInstance -break true ($softTex + ".colorEntryList[2]");
  344.                 setAttr ($softTex+".colorEntryList[0].color") -type double3 1.0 1.0 1.0 ;    
  345.                 setAttr ($softTex+".colorEntryList[1].color") -type double3  
  346.                         $transparency[0] $transparency[1] $transparency[2];
  347.                 setAttr ($softTex+".colorEntryList[0].position") 0.0 ;    
  348.                 setAttr ($softTex+".colorEntryList[1].position") $softness ;    
  349.                 setAttr ($softTex + ".type") 1;
  350.                 setAttr ($softTex + ".interpolation") 4;
  351.                 string $tform = `shadingNode -asUtility place2dTexture`;
  352.                 setAttr ($tform + ".repeatU" ) 2;
  353.                 setAttr ($tform + ".mirrorU" ) 1;
  354.                 connectAttr ($tform + ".outUV") ($softTex + ".uv");
  355.                 connectAttr ($tform + ".outUvFilterSize") ( $softTex + ".uvFilterSize" );
  356.                 if( $opTex ){
  357.                     if( $texType == 4 ){ 
  358.                         connectAttr -f ($texOp + ".outTransparency") 
  359.                                         ($softTex+".colorEntryList[1].color");    
  360.                     } else {
  361.                         string $reverse = `shadingNode -asUtility reverse`;
  362.                         connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputX");
  363.                         connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputY");
  364.                         connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputZ");
  365.                         connectAttr -f ($reverse + ".output")
  366.                                         ($softTex+".colorEntryList[1].color");    
  367.                     }
  368.                 } 
  369.                 $texOp = $softTex;
  370.             }
  371.             if( $opacityTex ){
  372.                 string $outAttr;
  373.                 if( $softnessTexture ) {
  374.                     $outAttr = ($texOp + ".outColor");
  375.                 } else if( $texType == 4 ){ 
  376.                     // only file texture has outTransparency
  377.                     $outAttr = ($texOp + ".outTransparency");
  378.                 } else {
  379.                     // We need to create a reverse node to convert alpha to transparency.
  380.                     // Hopefully the fan out here will not result in extra evaluations of
  381.                     // the texture per sample.
  382.                     string $reverse = `shadingNode -asUtility reverse`;
  383.                     connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputX");
  384.                     connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputY");
  385.                     connectAttr -f ($texOp + ".outAlpha") ($reverse + ".inputZ");
  386.                     $outAttr = ($reverse + ".output");
  387.                 }
  388.                 if( $useRampShader ){
  389.                     connectAttr $outAttr ($material + ".transparency[0].transparency_Color");
  390.                 }else{
  391.                     connectAttr $outAttr ($material + ".transparency");
  392.                 }
  393.             }
  394.     }
  395.     if( $hasSpecular && ($opacityTex || ($colorTex && $textureSpecular))){
  396.         // modulate specular  based on transparency
  397.         string $mult;
  398.         if( $opacityTex ){
  399.             $mult = `shadingNode -asUtility multiplyDivide`;
  400.             if( $softnessTexture ){
  401.                 string $reverse = `shadingNode -asUtility reverse`;
  402.                 connectAttr -f ($texOp + ".outColor") ($reverse + ".input");
  403.                 connectAttr -f ($reverse + ".output") ($mult + ".input2");
  404.             }else if( $texType == 4 ){
  405.                 string $reverse = `shadingNode -asUtility reverse`;
  406.                 connectAttr -f ($texOp + ".outTransparency") ($reverse + ".input");
  407.                 connectAttr -f ($reverse + ".output") ($mult + ".input2");
  408.             } else {
  409.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2X");
  410.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2Y");
  411.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2Z");
  412.             }
  413.             
  414.             setAttr ($mult + ".input1X") ($specularColor[0]*$specular);
  415.             setAttr ($mult + ".input1Y") ($specularColor[1]*$specular);
  416.             setAttr ($mult + ".input1Z") ($specularColor[2]*$specular);
  417.         }
  418.         if( $colorTex && $textureSpecular ){
  419.             string $mult2 = `shadingNode -asUtility multiplyDivide`;
  420.             connectAttr -f ($tex + ".outColor") ($mult2 + ".input2");
  421.             setAttr ($mult2 + ".input1X") ($specularColor[0]*$specular);
  422.             setAttr ($mult2 + ".input1Y") ($specularColor[1]*$specular);
  423.             setAttr ($mult2 + ".input1Z") ($specularColor[2]*$specular);
  424.             if( $opacityTex ){
  425.                 // combine opacity and color mults together
  426.                 string $mult3 = `shadingNode -asUtility multiplyDivide`;
  427.                 connectAttr -f ($mult + ".output") ($mult3 + ".input1");
  428.                 connectAttr -f ($mult2 + ".output") ($mult3 + ".input2");
  429.                 $mult = $mult3;
  430.             } else {
  431.                 $mult = $mult2;
  432.             }
  433.         }
  434.         if( $useRampShader ){
  435.             connectAttr -f ($mult + ".output") ($material + ".specularColor[0].specularColor_Color");
  436.         } else {
  437.             connectAttr -f ($mult + ".output") ($material + ".specularColor");
  438.         }
  439.     }
  440.     if( $hasIncand && $opacityTex ){
  441.         // modulate specular  based on transparency
  442.         string $mult;
  443.         if( $opacityTex ){
  444.             $mult = `shadingNode -asUtility multiplyDivide`;
  445.             if( $softnessTexture ){
  446.                 string $reverse = `shadingNode -asUtility reverse`;
  447.                 connectAttr -f ($texOp + ".outColor") ($reverse + ".input");
  448.                 connectAttr -f ($reverse + ".output") ($mult + ".input2");
  449.             }else if( $texType == 4 ){
  450.                 string $reverse = `shadingNode -asUtility reverse`;
  451.                 connectAttr -f ($texOp + ".outTransparency") ($reverse + ".input");
  452.                 connectAttr -f ($reverse + ".output") ($mult + ".input2");
  453.             } else {
  454.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2X");
  455.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2Y");
  456.                 connectAttr -f ($texOp + ".outAlpha") ($mult + ".input2Z");
  457.             }
  458.             
  459.             setAttr ($mult + ".input1X") ($incand[0]);
  460.             setAttr ($mult + ".input1Y") ($incand[1]);
  461.             setAttr ($mult + ".input1Z") ($incand[2]);
  462.         }
  463.         if( $useRampShader ){
  464.             connectAttr -f ($mult + ".output") ($material + ".incandescence[0].incandescence_Color");
  465.         } else {
  466.             connectAttr -f ($mult + ".output") ($material + ".incandescence");
  467.         }
  468.     }
  469.  
  470.     // create and assign shading group
  471.     $sg = `sets -renderable true -noSurfaceShader true -empty -name ($material + "SG")`;
  472.     defaultNavigation
  473.         -connectToExisting
  474.         -source $material
  475.         -destination $sg;
  476.     select -r $mesh;
  477.     hyperShade -assign $sg;
  478.  
  479.     if( $opacityTex && !$useRampShader ){
  480.         // set up connections for combined channels texturing
  481.         string $matInfo = getShaderMaterialInfo( $sg );
  482.         if( $matInfo != ""){
  483.             setAttr ($matInfo + ".texturePlug") -type "string" ($material + ".outColor");
  484.             connectAttr -f ($material + ".message") ($matInfo + ".texture[0]");
  485.         }
  486.     }
  487. }
  488.  
  489. //
  490. //  Procedure Name:
  491. //      doPaintEffectsToPoly
  492. //
  493. //  Description:
  494. //        This is the actual function that gets called from "PaintEffectsToPoly" 
  495. //      option box.
  496. //
  497. //  Input Arguments:
  498. //    ConstructionHistory  0 = OFF, 1 = ON
  499. //    VertexColorMode 0 = NONE, 1 = Color, 2 = Illuminated    
  500. //    QuadOutput 0 = triangles, 1 = quads
  501. //
  502. //  Return Value:
  503. //      None.
  504. //
  505. //
  506. global proc int doPaintEffectsToPoly( int $constructionHistory, int $vertColorMode,
  507.                                       int $quadOutput, int $hideStrokes, int $polyLimit)
  508. {
  509.     int $status = 0;
  510.  
  511.     string $strokes[] = `ls -sl -dag -type stroke`;
  512.     int $len = size($strokes);
  513.     if( $len < 1 ){
  514.         error( "Select one or more strokes to tessellate." );
  515.         return $status;
  516.     }
  517.     string $newMeshes[];
  518.     string $selectedMeshes[];
  519.     int $meshCount = 0;
  520.     int $i;    
  521.     string $perspCameras[] = `listCameras -p`;
  522.  
  523.     // always create shaders with textures.. originally we didn't do this if colorPerVertex was used.
  524.     int $createTexturedShaders = true; 
  525.  
  526.     for($i = 0; $i < $len; $i++) {
  527.         string $stroke = $strokes[$i];
  528.  
  529.         int $vis = `getAttr  ($stroke + ".visibility")`;
  530.         if (!$vis){
  531.              continue;
  532.         }
  533.         // get the stroke's brush
  534.         string $attr = $stroke + ".brush";
  535.         string $result = `connectionInfo -sfd $attr`;
  536.         if( size( $result ) < 1 ){
  537.             continue; // skip if stroke has no brush
  538.         }
  539.         string $buffer[];
  540.         int $num = `tokenize $result "." $buffer`;
  541.         string $brush = $buffer[0];
  542.         int $brushType = getAttr( $brush + ".brushType" );
  543.         int $tubes = getAttr( $brush + ".tubes" );
  544.         int $hardEdges = getAttr( $brush + ".hardEdges" );
  545.         
  546.         setAttr ($stroke + ".meshVertexColorMode") $vertColorMode;
  547.         setAttr ($stroke + ".meshQuadOutput") $quadOutput;
  548.         setAttr ($stroke + ".meshHardEdges") $hardEdges;
  549.         setAttr ($stroke + ".meshPolyLimit") $polyLimit;
  550.  
  551.         // set up mesh parent xform
  552.         string $meshParent = `createNode transform -name ($brush+"MeshGroup") `;
  553.  
  554.         // setup mainMesh
  555.         string $mainParent = `createNode transform -name ($brush+"Main") -p $meshParent`;
  556.         string $mainMesh = `createNode mesh -n ($brush+"MainShape") -p $mainParent`;
  557.  
  558.  
  559.         // make sure we get the full path, since a name that's unique now
  560.         // might not be unique by the time we're done
  561.         $selectedMeshes = `selectedNodes`;
  562.         $newMeshes[$meshCount] = $selectedMeshes[0];
  563.  
  564.         $meshCount++;
  565.         connectAttr ($stroke + ".worldMainMesh[0]") ($mainMesh + ".inMesh");
  566.         float $incand1[] = `getAttr ($brush + ".incandescence1")`;
  567.         float $incand2[] = `getAttr ($brush + ".incandescence2")`;
  568.         float $transp1[] = `getAttr ($brush + ".transparency1")`;
  569.         float $transp2[] = `getAttr ($brush + ".transparency2")`;
  570.         float $transp[3];
  571.         float $softness = getAttr( $brush + ".softness" );
  572.         float $glow = getAttr( $brush + ".glow" );
  573.         float $shaderGlow = getAttr( $brush + ".shaderGlow" );
  574.         float $glowIntensity = $glow + $shaderGlow;
  575.         float $fractalAmplitude = getAttr( $brush + ".fractalAmplitude" );
  576.         float $fractalRatio = getAttr( $brush + ".fractalRatio" );
  577.         float $fractalThreshold = getAttr( $brush + ".fractalThreshold" );
  578.         if( $tubes ){
  579.             $transp[0] = ($transp1[0] + $transp2[0]) * 0.5;
  580.             $transp[1] = ($transp1[1] + $transp2[1]) * 0.5;
  581.             $transp[2] = ($transp1[2] + $transp2[2]) * 0.5;
  582.             $incand1[0] = ($incand1[0] + $incand2[0]) * 0.5;
  583.             $incand1[1] = ($incand1[1] + $incand2[1]) * 0.5;
  584.             $incand1[2] = ($incand1[2] + $incand2[2]) * 0.5;
  585.         } else {
  586.             $transp = $transp1;
  587.         }
  588.         int $textureSpecular = false;
  589.         if( $brushType < 4 ){
  590.             // When converting stamp type brushes to mesh
  591.             // we do not usually want low levels of softness
  592.             // and transparency.
  593.             if( $softness > -0.3 && $softness < 0.3 ){
  594.                 $softness = 0.0;
  595.             }
  596.             if( $transp[0] < 0.2 ){
  597.                 $transp[0] = 0.0;
  598.             }
  599.             if( $transp[1] < 0.2 ){
  600.                 $transp[1] = 0.0;
  601.             }
  602.             if( $transp[2] < 0.2 ){
  603.                 $transp[2] = 0.0;
  604.             }
  605.             // Stamp brushes apply any color textures to specular
  606.             // as well.
  607.             $textureSpecular = true;
  608.         }
  609.  
  610.         float $specularColor[] = `getAttr ($brush + ".specularColor")`;
  611.         float $specularPower = `getAttr ($brush + ".specularPower")`;
  612.         if( $specularPower < 2.0 ){
  613.             $specularPower = 2.0; // minimum on phong node
  614.         }
  615.         int $texType = `getAttr($brush + ".textureType")`;
  616.         int $colorTex = `getAttr($brush + ".mapColor")`;
  617.         int $opacityTex = `getAttr($brush + ".mapOpacity")`;
  618.         float $repeatU = `getAttr($brush + ".repeatU")`;
  619.         float $repeatV = `getAttr($brush + ".repeatV")`;
  620.         float $offsetU = `getAttr($brush + ".offsetU")`;
  621.         float $offsetV = `getAttr($brush + ".offsetV")`;
  622.         string $texName = `getAttr($brush + ".imageName")`;
  623.  
  624.         int $linkCamera = `getAttr( $brush + ".forwardTwist" )`;
  625.         float $flatness1 = `getAttr($brush + ".flatness1" )`;
  626.         float $flatness2 = `getAttr($brush + ".flatness2" )`;
  627.         int $isFlat;
  628.         if( $brushType < 4 ){
  629.             $isFlat =  $flatness1 > 0.7 && (($tubes==0) || $flatness2 > 0.7);
  630.         } else {
  631.             $isFlat =  $flatness1 > 0.999 && (($tubes==0) || $flatness2 > 0.999);
  632.         }
  633.  
  634.         if( !$linkCamera && ($brushType < 4 ) && $opacityTex ){
  635.             // to better simulate the 2D map method we use forwardTwist
  636.             // when converting stamp style brushes. TstrokeShape has the
  637.             // same checks as the code below and sets forward twist on
  638.             // during the pfxToPoly call.
  639.             int  $mapMethod = `getAttr($brush + ".mapMethod" )`;
  640.             if( $mapMethod == 2 ){
  641.                 if( $flatness1 < 0.05 && (($tubes==0) || $flatness2 < 0.05 )){
  642.                     $linkCamera = true;
  643.                     $isFlat = true;
  644.                 }
  645.             } 
  646.         } 
  647.  
  648.         if( $createTexturedShaders ){
  649.             // create shader to match branch rendering by brush
  650.             float $color1[] = `getAttr ($brush + ".color1")`;
  651.             float $color2[] = $color1;
  652.             if( $tubes ){
  653.                 $color2 = `getAttr ($brush + ".color2")`;
  654.  
  655.                 // We try to mimick depth shadow effect
  656.                 // by darkening color1.
  657.                 float $depthShadow = `getAttr($brush + ".depthShadow")`;    
  658.                 if( $depthShadow > 0.05 ){
  659.                     float $lengthMax = `getAttr($brush + ".lengthMax")`;    
  660.                     float $depthShadowDepth = `getAttr($brush + ".depthShadowDepth")`;    
  661.                     float $weight = $depthShadowDepth/$lengthMax;    
  662.                     if( $weight > 0 ){
  663.                         if( $weight < 1.0 ){
  664.                             $depthShadow *= $weight;
  665.                         } 
  666.                         $depthShadow = 1.0-$depthShadow;
  667.                         $color1[0] *= $depthShadow;
  668.                         $color1[1] *= $depthShadow;
  669.                         $color1[2] *= $depthShadow;
  670.                     }
  671.                 }
  672.             }
  673.             float $specular = `getAttr ($brush + ".specular")`;
  674.             float $translucence = `getAttr ($brush + ".translucence")`;
  675.             float $smearU = `getAttr ($brush + ".smearU")`;
  676.             float $smearV = `getAttr ($brush + ".smearV")`;
  677.             float $smear = `getAttr ($brush + ".smear")`;
  678.             float $texColor1[] = `getAttr ($brush + ".texColor1")`;
  679.             float $texColor2[] = `getAttr ($brush + ".texColor2")`;
  680.             float $reflectivity = 0.0;        
  681.             if( $brushType == 5 ){ // mesh brush
  682.                 $reflectivity = `getAttr ($brush + ".branchReflectivity")`;
  683.             }
  684.         
  685.             createAndAssignShader(($brush + "Shader"), $color1, $color2,
  686.                         $incand1, $specularColor, $specular, $specularPower, $glowIntensity,
  687.                         $translucence, $transp, $softness,
  688.                         $texType, $colorTex, $opacityTex, $texName, 
  689.                         $texColor1, $texColor2, $smear, $smearU, $smearV,
  690.                         $fractalRatio, $fractalAmplitude, $fractalThreshold,
  691.                         $repeatU, $repeatV, $offsetU, $offsetV, 
  692.                         $reflectivity, $textureSpecular, $isFlat, $brush, $mainMesh );
  693.  
  694.         } else {
  695.             // color per vertex.. create simple surface shader 
  696.             string $material = `shadingNode -asShader "lambert"`;
  697.             // create and assign shading group
  698.             string $sg = `sets -renderable true -noSurfaceShader true -empty -name ($material + "SG")`;
  699.             defaultNavigation
  700.                 -connectToExisting
  701.                 -source $material
  702.                 -destination $sg;
  703.             select -r $mainMesh;
  704.             hyperShade -assign $sg;
  705.         }
  706.  
  707.         if( !$constructionHistory ){
  708.             delete -ch $mainMesh;
  709.         }
  710.     
  711.  
  712.     
  713.         // setup leafMesh
  714.         int $leaves = ($tubes && getAttr( $brush + ".leaves" ));
  715.         if( $leaves ){
  716.             if( getAttr( $brush + ".leafForwardTwist" )) {
  717.                 $linkCamera = true;
  718.             }
  719.             int $useBranch = `getAttr($brush + ".leafUseBranchTex")`;
  720.             float $flatness = `getAttr($brush + ".leafFlatness" )`;
  721.             $isFlat = $flatness > 0.999
  722.                     || ($brushType < 4 && $flatness > 0.7);
  723.             if( !$linkCamera && ($brushType < 4 ) && !$useBranch ){
  724.                 // to better simulate the 2D map method we use forwardTwist
  725.                 // when converting stamp style brushes. TstrokeShape has the
  726.                 // same checks as the code below and sets forward twist on
  727.                 // during the pfxToPoly call.
  728.                 if( $flatness < 0.3 ){
  729.                     string $tName = `getAttr ($brush + ".leafImage")`;
  730.                     if( size( $tName ) > 0 ){
  731.                         $linkCamera = true;
  732.                         $isFlat = true;
  733.                     }
  734.                 } 
  735.             } 
  736.             string $leafParent = `createNode transform -name ($brush+"Leaf") -p $meshParent`;
  737.             string $leafMesh = `createNode mesh -name ($brush+"LeafShape") -p $leafParent`;
  738.  
  739.             // make sure we get the full path, since a name that's unique now
  740.             // might not be unique by the time we're done
  741.             $selectedMeshes = `selectedNodes`;
  742.             $newMeshes[$meshCount] = $selectedMeshes[0];
  743.  
  744.             $meshCount++;
  745.             connectAttr ($stroke + ".worldLeafMesh[0]") ($leafMesh + ".inMesh");
  746.             if( $createTexturedShaders ){
  747.                 // create shader to match leaf rendering by brush
  748.                 float $color1[] = `getAttr ($brush + ".leafColor1")`;
  749.                 float $color2[] = `getAttr ($brush + ".leafColor2")`;
  750.                 float $specular = `getAttr ($brush + ".leafSpecular")`;
  751.                 float $translucence = `getAttr ($brush + ".leafTranslucence")`;
  752.                 int $tType = $texType;
  753.                 int $colTex = $colorTex;
  754.                 int $opTex = $opacityTex;
  755.                 string $tName = $texName;
  756.                 float $repU = $repeatU;
  757.                 float $repV = $repeatV;
  758.                 float $offU = $offsetU;
  759.                 float $offV = $offsetV;
  760.                 if( !$useBranch ){
  761.                     $tName = `getAttr ($brush + ".leafImage")`;
  762.                     if( size( $tName ) > 0 ){
  763.                         $colTex = true;
  764.                         $opTex = true;
  765.                         $tType = 4;
  766.                         $repU = 1.0;
  767.                         $repV = 1.0;
  768.                         $offU = 0.0;
  769.                         $offV = 0.0;
  770.                     } else {
  771.                         $colTex = false;
  772.                         $opTex = false;
  773.                     }
  774.                 }
  775.                 float $smearU = 0.0;
  776.                 float $smearV = 0.0;
  777.                 float $smear = 1.0;
  778.                 float $texColor1[3];
  779.                 float $reflectivity = 0.0;
  780.                 if( $brushType == 5 ){ // mesh brush
  781.                     $reflectivity = `getAttr ($brush + ".leafReflectivity")`;
  782.                 }
  783.                 $texColor1[0] = $texColor1[1] = $texColor1[2] = 1.0;
  784.                 $texColor2[0] = $texColor2[1] = $texColor2[2] = 0.0;
  785.                 createAndAssignShader(($brush + "LeafShader"), $color1, $color2,
  786.                         $incand1, $specularColor, $specular, $specularPower, $glowIntensity,
  787.                         $translucence, $transp, $softness,
  788.                         $tType, $colTex, $opTex, $tName, 
  789.                         $texColor1, $texColor2, $smear, $smearU, $smearV,
  790.                         $fractalRatio, $fractalAmplitude, $fractalThreshold,
  791.                         $repU, $repV, $offU, $offV, 
  792.                         $reflectivity, $textureSpecular, $isFlat, $brush, $leafMesh );
  793.             } else {
  794.                 // color per vertex.. create simple surface shader 
  795.                 string $material = `shadingNode -asShader "lambert"`;
  796.                 // create and assign shading group
  797.                 $sg = `sets -renderable true -noSurfaceShader true -empty -name ($material + "SG")`;
  798.                 defaultNavigation
  799.                     -connectToExisting
  800.                     -source $material
  801.                     -destination $sg;
  802.                 select -r $leafMesh;
  803.                 hyperShade -assign $sg;
  804.             }
  805.  
  806.             if( !$constructionHistory ){
  807.                 delete -ch $leafMesh;
  808.             }
  809.         }
  810.  
  811.  
  812.  
  813.  
  814.         // setup flowerMesh
  815.         int $flowers = ($tubes && getAttr( $brush + ".flowers" ));
  816.         if( $flowers ){
  817.             if( getAttr( $brush + ".petalForwardTwist" )) {
  818.                 $linkCamera = true;
  819.             }
  820.             int $useBranch = `getAttr($brush + ".flowerUseBranchTex")`;
  821.             float $flatness = `getAttr($brush + ".petalFlatness" )`;
  822.             $isFlat = $flatness > 0.999
  823.                     || ($brushType < 4 && $flatness > 0.7);
  824.             if( !$linkCamera && ($brushType < 4 ) && !$useBranch ){
  825.                 // to better simulate the 2D map method we use forwardTwist
  826.                 // when converting stamp style brushes. TstrokeShape has the
  827.                 // same checks as the code below and sets forward twist on
  828.                 // during the pfxToPoly call.
  829.                 if( $flatness < 0.8 ){
  830.                     string $tName = `getAttr ($brush + ".flowerImage")`;
  831.                     if( size( $tName ) > 0 ){
  832.                         $linkCamera = true;
  833.                         $isFlat = true;
  834.                     }
  835.                 } 
  836.             } 
  837.             string $flowerParent = `createNode transform -name ($brush+"Flower") -p $meshParent`;
  838.             string $flowerMesh = `createNode mesh -name ($brush+"FlowerShape") -p $flowerParent`;
  839.             // make sure we get the full path, since a name that's unique now
  840.             // might not be unique by the time we're done
  841.             $selectedMeshes = `selectedNodes`;
  842.             $newMeshes[$meshCount] = $selectedMeshes[0];
  843.  
  844.             $meshCount++;
  845.             connectAttr ($stroke + ".worldFlowerMesh[0]") ($flowerMesh + ".inMesh");
  846.  
  847.             if( $createTexturedShaders ){
  848.                 // create shader to match flower rendering by brush
  849.                 float $color1[] = `getAttr ($brush + ".petalColor1")`;
  850.                 float $color2[] = `getAttr ($brush + ".petalColor2")`;
  851.                 float $specular = `getAttr ($brush + ".flowerSpecular")`;
  852.                 float $translucence = `getAttr ($brush + ".flowerTranslucence")`;
  853.                 int $tType = $texType;
  854.                 int $colTex = $colorTex;
  855.                 int $opTex = $opacityTex;
  856.                 string $tName = $texName;
  857.                 float $repU = $repeatU;
  858.                 float $repV = $repeatV;
  859.                 float $offU = $offsetU;
  860.                 float $offV = $offsetV;
  861.                 if( !$useBranch ){
  862.                     $tName = `getAttr ($brush + ".flowerImage")`;
  863.                     if( size( $tName ) > 0 ){
  864.                         $colTex = true;
  865.                         $opTex = true;
  866.                         $tType = 4;
  867.                         $repU = 1.0;
  868.                         $repV = 1.0;
  869.                         $offU = 0.0;
  870.                         $offV = 0.0;
  871.                     } else {
  872.                         $colTex = false;
  873.                         $opTex = false;
  874.                     }
  875.                 }
  876.                 float $smearU = 0.0;
  877.                 float $smearV = 0.0;
  878.                 float $smear = 1.0;
  879.                 float $texColor1[3];
  880.                 float $texColor1[3];
  881.                 float $reflectivity = 0.0;
  882.                 if( $brushType == 5 ){ // mesh brush
  883.                     $reflectivity = `getAttr ($brush + ".flowerReflectivity")`;
  884.                 }
  885.                 $texColor1[0] = $texColor1[1] = $texColor1[2] = 1.0;
  886.                 $texColor2[0] = $texColor2[1] = $texColor2[2] = 0.0;
  887.                 createAndAssignShader(($brush + "FlowerShader"), $color1, $color2,
  888.                         $incand1, $specularColor, $specular, $specularPower, $glowIntensity,
  889.                         $translucence, $transp, $softness,
  890.                         $tType, $colTex, $opTex, $tName, 
  891.                         $texColor1, $texColor2, $smear, $smearU, $smearV,
  892.                         $fractalRatio, $fractalAmplitude, $fractalThreshold,
  893.                         $repU, $repV, $offU, $offV, 
  894.                         $reflectivity, $textureSpecular, $isFlat, $brush, $flowerMesh );
  895.  
  896.             } else {
  897.                 // color per vertex.. create simple surface shader 
  898.                 string $material = `shadingNode -asShader "lambert"`;
  899.                 // create and assign shading group
  900.                 $sg = `sets -renderable true -noSurfaceShader true -empty -name ($material + "SG")`;
  901.                 defaultNavigation
  902.                     -connectToExisting
  903.                     -source $material
  904.                     -destination $sg;
  905.                 select -r $flowerMesh;
  906.                 hyperShade -assign $sg;
  907.             }
  908.  
  909.             if( !$constructionHistory ){
  910.                 delete -ch $flowerMesh;
  911.             }
  912.         }
  913.         if($hideStrokes){
  914.             setAttr  ($stroke + ".visibility") 0;
  915.         }
  916.         if($linkCamera && size($perspCameras) > 0){
  917.             connectAttr -f ($perspCameras[0] + ".translate") ($stroke + ".cameraPoint");
  918.         }
  919.         
  920.     }    
  921.     select -r $newMeshes;
  922.     $status = 1;
  923.  
  924.     return $status;
  925. }
  926.  
  927.  
  928.